package v1

import (
	"strconv"
	"wms/internal/model/request"
	"wms/internal/service"
	"wms/pkg/common"
	"wms/pkg/logger"

	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"gorm.io/gorm"
)

// OutboundController 出库控制器
type OutboundController struct {
	outboundService *service.InventoryService
}

// NewOutboundController 创建出库控制器
func NewOutboundController(db *gorm.DB, logger *zap.Logger) *OutboundController {
	return &OutboundController{
		outboundService: service.NewInventoryService(db, logger),
	}
}

// UpdateOutboundStatus 更新出库单状态
// UpdateOutboundStatus godoc
// @Summary 更新出库单状态
// @Description 更新指定出库单的状态信息
// @Tags 出库管理
// @Accept json
// @Produce json
// @Security Bearer
// @Param id path int true "出库单ID"
// @Param request body request.UpdateStatusRequest true "状态更新信息"
// @Success 200 {object} common.Response "成功"
// @Failure 400 {object} common.Response "请求错误"
// @Failure 500 {object} common.Response "内部错误"
// @Router /outbound/{id}/status [put]
func (c *OutboundController) UpdateOutboundStatus(ctx *gin.Context) {
	id, err := strconv.ParseUint(ctx.Param("id"), 10, 32)
	if err != nil {
		logger.Warn("解析出库单ID失败", zap.Error(err))
		common.Error(ctx, err)
		return
	}

	var req request.UpdateStatusRequest
	if err := ctx.ShouldBindJSON(&req); err != nil {
		logger.Warn("绑定状态更新请求参数失败", zap.Error(err))
		common.Error(ctx, err)
		return
	}

	logger.Info("收到更新出库单状态请求",
		zap.Uint("orderID", uint(id)),
		zap.String("newStatus", req.Status))

	userID := common.GetUserID(ctx)

	if err := c.outboundService.UpdateOutboundStatus(uint(id), &req, userID); err != nil {
		logger.Error("更新出库单状态失败", zap.Uint("orderID", uint(id)), zap.Error(err))
		common.Error(ctx, err)
		return
	}

	common.Success(ctx, "出库单状态更新成功")
}
